<?php
class Manager_RoleController extends Zend_Controller_Action
{
	private $_output;
	
	public function init()
	{
		Zend_Loader::loadClass('RoleModel');
		Zend_Loader::loadClass('RoleForm');
		$this->view->controller = $this->_request->getControllerName();
		$this->_output = new Custom_Controller_Plugin_Output();
	}
	
	public function listAction()
	{
		$action = $this->_request->getParam('a');
		
		switch($action)
		{
			case 'rows':
				$this->_helper->layout()->disableLayout();
				$this->_helper->viewRenderer->setNoRender(true);
				
				if($this->_request->isPost())
				{
					$post = $this->_request->getPost();
					$role = new RoleModel();
					$rows = $role->pageRow($post);
					
					$this->_output->grid($rows);
				}
				break;
		}
	}
	
	public function addAction()
	{
		$action = $this->_request->getParam('a');
		
		switch($action)
		{
			case 'submit':
				$this->_helper->layout()->disableLayout();
				$this->_helper->viewRenderer->setNoRender(true);
				
				if($this->_request->isPost())
				{
					$post = $this->_request->getPost();
					$role = new RoleModel();
					$role->addRole($post);
				}
				break;
		}
	}
	
	public function editAction()
	{
		$action = $this->_request->getParam('a');
		
		switch($action)
		{
			case 'submit':
				$this->_helper->layout()->disableLayout();
				$this->_helper->viewRenderer->setNoRender(true);
				
				if($this->_request->isPost())
				{
					$post = $this->_request->getPost();
					$role = new RoleModel();
					$role->editRole($post['id'], $post);
				}
				break;
				
			case 'permission':
				$this->_helper->layout()->disableLayout();
				$this->_helper->viewRenderer->setNoRender(true);
				
				if($this->_request->isPost())
				{
					$post = $this->_request->getPost();
					$role = new RoleModel();
					$role->getPermission($post['id'], $post);
				}
				
				break;
				
			default:
				$id = $this->_request->getParam('id');
				$role = new RoleModel();
				$this->view->row = $role->row($id);
				break;
		}
	}
	
	public function deleteAction()
	{
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		
		if($this->_request->isPost())
		{
			$post = $this->_request->getPost();
			$role = new RoleModel();
			$role->deleteRole($post['id']);
		}
	}
}